package com.java.project_2.CustomerList;

import com.java.project_2.Customerbean.Customer;

/**
 * @author luzhikun
 * @date 2020/11/30- 15:51
 */
public class CustomerList {

    private Customer[] customers;     //用来保存对象的数组
    private int total;                  //用来记录客户的数量

    /**
     * 用来初始化这个数组
     *
     * @param totalCustomers
     */
    public CustomerList(int totalCustomers) {
        customers = new Customer[totalCustomers];
    }

    /**
     * 用来删除客户的
     *
     * @param index
     * @return
     */
    public boolean deleCustomer(int index) {
        if (index < 0 || index >= total) {
            return false;
        } else {
            for (int i = index; i < total; i++) {
                customers[index] = customers[total + 1];
            }
            customers[total] = null;
            total--;
            return true;
        }
    }

    /**
     * 用来添加用户的
     *
     * @param customer
     * @return
     */
    public boolean addCustomer(Customer customer) {
        if (total > customers.length) {
            return false;
        } else {
            customers[total++] = customer;
            return true;
        }
    }

    /**
     * 用来修改客户信息的
     *
     * @param index
     * @param cust
     * @return
     */
    public boolean replCustomer(int index, Customer cust) {
        if (index < 0 || index >= total) {
            return false;
        } else {
            customers[index] = cust;
            return true;
        }
    }

    /**
     * 用来获取所有客户信息的
     *
     * @return
     */
    public Customer[] getAllCustomers() {
        Customer[] cust = new Customer[total];
        for (int i = 0; i < total; i++) {
            cust[i] = customers[i];
        }
        return cust;
    }

    /**
     * 用来获取指定客户信息的
     *
     * @param index
     * @return
     */
    public Customer getCustomers(int index) {
        if (index < 0 || index >= total) {
            return null;
        } else {
            return customers[index];
        }
    }

    public int getTotal() {
        return total;
    }
}
